#include <func.h>

int cmd_separation(char* cmd_string, char* cmd, char* arg) {
    char* p = cmd_string;
    int i = 0;
    while ((*p) != ' ' && (*p) != '\n'&&(*p)!='\0') {//分离出命令存到cmd
        cmd[i] = *p;
        p++;
        ++i;
    }
    if (*p=='\0') {//无参命令
        arg = "\0";
        return 0;
    }
    p++;

    i = 0;
    while (*p != '\0') {
        arg[i++] = *p;
        p++;
    }

    return 0;
}

int is_cmd_legal(char* cmd, char** cmd_table,int len) {

    int i = 0;
    while (i<len) {
        if (strcmp(cmd, cmd_table[i])==0) {
            return 0;
        }
        i++;
    }

    return -1;
}

//unit test
int main(int argc, char* argv[])
{
    char* cmd_table[] = { "ls","cd","gets","puts","pwd","remove" };
    char cmd[1024] = { 0 };
    char arg[10224] = { 0 };
    cmd_separation("ikun rap lanqiu", cmd, arg);
    puts(cmd);
    puts(arg);
    int len = SIZE(cmd_table);

    int ret = is_cmd_legal(cmd, cmd_table,len);
    printf("ret= %d\n", ret);
    return 0;
}


